package com.lagou.controller;

import com.github.pagehelper.PageInfo;
import com.lagou.domain.PromotioAdVO;
import com.lagou.domain.PromotionAd;
import com.lagou.domain.ResponseResult;
import com.lagou.service.PromotionAdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/PromotionAd")
public class PromotionAdController {

   @Autowired
   private PromotionAdService promotionAdService;
   /*
   * 广告分页查询
   * */
   @RequestMapping("/findAllPromotionAdByPage")
   public ResponseResult findAllByPage( PromotioAdVO promotioAdVO) {

       PageInfo<PromotionAd> pageInfo = promotionAdService.findAllPromotionAdByPage (promotioAdVO);

       ResponseResult result = new ResponseResult (true, 200, "广告分页查询成功", pageInfo);

       return result;

   }

    @RequestMapping("/PromotionAdUpload")
    public ResponseResult fileUpload(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws IOException {

        //1.判断文件是否为空
        if (file.isEmpty ()) {
            throw new RuntimeException ();
        }
        //2.获取项目部署路径
        String realPath = request.getServletContext ().getRealPath ("/");
        String substring = realPath.substring (0, realPath.indexOf ("ssm_web"));

        //3.获取原文件名
        String originalFilename = file.getOriginalFilename ();

        //4.生成新文件名
        String newFileName = System.currentTimeMillis () + originalFilename.substring (originalFilename.lastIndexOf ("."));

        //5.文件上传
        String uploadPath = substring + "upload\\";
        File filePath = new File(uploadPath, newFileName);


        //如果目录不存在就创建目录
        if (!filePath.getParentFile ().exists ()){
            filePath.getParentFile ().mkdirs ();
            System.out.println ("创建目录" + filePath);
        }

        //图片就进行了真正的上传
        file.transferTo (filePath);

        //6.将文件名和文件路径返回，进行响应
        Map<String,String> map = new HashMap<> ();
        map.put ("fileName",newFileName);
        map.put("filePath","http://localhost:8080/upload" + newFileName);
        ResponseResult result = new ResponseResult (true, 200, "响应成功", map);
        return result;
    }

    /*广告状态的上下线*/

    @RequestMapping("/updatePromotionAdStatus")
    public ResponseResult updatePromotionAdStatus(int id,int status){

        promotionAdService.updatePromotionAdStatus (id, status);

        ResponseResult result = new ResponseResult (true, 200, "响应成功", null);

        return result;
    }
}
